﻿using System.Collections.Generic;

namespace HEFramework
{
    public enum eTimerClass
    {
        None,
        OnceTimer,
        OnceWaitTimer,
        RepeatedTimer,
    }

    public class TimerAction
    {
        public static TimerAction Create(long _id, eTimerClass _eTimerClass, long _startTime, long _time, int _type, object _obj)
        {
            TimerAction timerAction = ObjectGenerater.Instance.Fetch<TimerAction>();
            timerAction.Id = _id;
            timerAction.TimerClass = _eTimerClass;
            timerAction.Object = _obj;
            timerAction.StartTime = _startTime;
            timerAction.Time = _time;
            timerAction.Type = _type;
            return timerAction;
        }

        public long Id { get; set; }
        public eTimerClass TimerClass { get; set; }
        public object Object { get; set; }
        public long StartTime { get; set; }
        public long Time { get; set; }
        public int Type { get; set; }

        public void Recycle()
        {
            this.Id = 0;
            this.Object = null;
            this.StartTime = 0;
            this.Time = 0;
            this.TimerClass = eTimerClass.None;
            this.Type = 0;
            ObjectGenerater.Instance.Recycle(this);
        }
    }

    public struct TimerCallback
    {
        public object args;
    }

    public class TimerComponent : Entity, IAwake, IUpdate
    {
        /// <summary>
        /// key: time, value: timer id
        /// </summary>
        public readonly MultiMap_List<long, long> timeId = new(1000);

        public readonly Queue<long> timeOutTime = new();

        public readonly Queue<long> timeOutTimerIds = new();

        public readonly Dictionary<long, TimerAction> timerActions = new();

        public long IdGenerator { get; set; }

        /// <summary>
        /// 记录最小时间，不用每次都去MultiMap取第一个值
        /// </summary>
        public long MinTime { get; set; } = long.MaxValue;
    }
}